package appClientModule;

public class TestUtil {
	
	public static void main(String args[]) {
		TestUtil tu = new TestUtil();
		String ret = "00D703D6E9865C806701";//
//		ret = "00DB038A93055C806701"; //9000
//		460030981334017
		System.out.println(tu.rValue1("12"));
		tu.f22ToImsi(ret);
	}
	public byte[] byteConvert(byte[] src) {
        byte []response = new byte[src.length];
        for(int i=0;i<response.length;i++){
        	response[i] = src[response.length-1-i];	
        }
        return response;
    }
	public String f22ToImsi(String ret) {
		String MIN_A = ret.substring(2, 6); 
		String MIN_B = ret.substring(6,12);
		String CC = ret.substring(12,14);
		String DDDD = ret.substring(16,ret.length());
		StringBuffer sb = new StringBuffer();
		byte[] min_a = MsgGenUtil.BCD2Bytes(MIN_A);
		min_a = byteConvert(min_a);
		String min_value = ""+(MsgGenUtil.makeInt(min_a)+111);
		String min = rValue3(min_value);
		
		byte[] min_b = MsgGenUtil.BCD2Bytes(MIN_B);
		min_b = byteConvert(min_b);
		int d = MsgGenUtil.makeInt(min_b);
		String BBBBBB = Integer.toBinaryString(d);

		String ett = BBBBBB.substring(BBBBBB.length()-10,BBBBBB.length());//8~10
		min_value = ""+(Integer.valueOf(ett, 2)+111);
		ett = rValue3(min_value);
		
		String serven = BBBBBB.substring(BBBBBB.length()-10-4,BBBBBB.length()-10);//8~10
		serven = ""+(Integer.valueOf(serven, 2));
		serven = rValue1(serven);
	
		String four = BBBBBB.substring(0,BBBBBB.length()-10-4);//8~10
		four = ""+(Integer.valueOf(four, 2)+111);
		four = rValue3(four);
		
		byte[] min_c = MsgGenUtil.BCD2Bytes(CC);
		String mnc = ""+(min_c[0]+11);
		mnc = rValue2(mnc);
		
		byte[] min_d = MsgGenUtil.BCD2Bytes(DDDD);
		min_d = byteConvert(min_d);
		String mcc = ""+(MsgGenUtil.makeInt(min_d)+111);
		mcc = rValue3(mcc);
		
		sb.append(mcc);//3
		sb.append(mnc);//2
		sb.append(min);//3
		sb.append(four);//1
		sb.append(serven);//2
		sb.append(ett);//3

		System.out.println(sb.toString());
		return sb.toString();
	}
	public String rValue2(String v){
		String min_value = v;	
		int two = Integer.parseInt(min_value.substring(min_value.length()-1, min_value.length()));
		if(two == 0)
			min_value = ""+(Integer.parseInt(min_value)-10);
		
		int one = Integer.parseInt(min_value.substring(0, min_value.length()-1));
		if(one == 0 || one == 10)
			min_value = ""+(Integer.parseInt(min_value)-10*10);
		//System.out.println(min_value);
		for(int i=min_value.length();i<2;i++){
			min_value= "0"+min_value;
		}
		return min_value;
	}
	public String rValue3(String v){
		String min_value = v;
		
		int three = Integer.parseInt(min_value.substring(min_value.length()-1, min_value.length()));
		if(three == 0)
			min_value = ""+(Integer.parseInt(min_value)-10);
		
		int two = Integer.parseInt(min_value.substring(min_value.length()-2, min_value.length()-1));
		if(two == 0)
			min_value = ""+(Integer.parseInt(min_value)-10*10);
		int	one = Integer.parseInt(min_value.substring(0, min_value.length()-2));
		if(one == 0 || one == 10)
			min_value = ""+(Integer.parseInt(min_value)-10*100);
		for(int i=min_value.length();i<3;i++) {
			min_value = "0"+min_value;
		}
		return min_value;
	}
	public String rValue1(String v){
		String min_value = v;
		int one = Integer.parseInt(min_value.substring(0, min_value.length()));
		if(one >= 10)
			min_value = ""+(Integer.parseInt(min_value)%10);
		return min_value;
	}
}
